﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace laohan.Common.MQ.RabbitMQ
{
    /// <summary>
    /// 
    /// </summary>
    public class Function
    {
        /// <summary>
        /// 
        /// </summary>
        public static RabbitMQRunner runner { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public static void InitRabbitMQ(string hostName, string userName, string password, bool isEnable, int port, string computerName)
        {
            try
            {
                if (runner != null)
                {
                    return;
                }

                RabbitMQClose();

                if (!isEnable)
                {
                    return;
                }

                runner = new RabbitMQRunner();

                runner.InitParameter(hostName, userName, password, port);
                runner.Init(computerName);
            }
            catch (Exception ex)
            {
                MessageBox.Show("初始化RabbitMQ发生错误，错误原因：" + ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="queue"></param>
        /// <param name="msg"></param>
        public static void RabbitMQSendMsg(string queue, string msg, bool noTime = false)
        {
            try
            {
                if (runner != null)
                {
                    if (!noTime)
                    {
                        msg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ">" + msg;
                    }

                    runner.SendMsg(queue, msg);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("发送消息发生错误，错误原因：" + ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="queue"></param>
        /// <param name="msg"></param>
        public static void RabbitMQSendMsgFanout(string exchange, string msg, bool noTime = false)
        {
            try
            {
                if (runner != null)
                {
                    if (!noTime)
                    {
                        msg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ">" + msg;
                    }

                    runner.SendMsgExchangeFanout(exchange, msg);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("发送消息发生错误，错误原因：" + ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="queue"></param>
        /// <param name="msg"></param>
        public static void RabbitMQSendMsgFanout(object exchange, string msg, bool noTime = false)
        {
            RabbitMQSendMsgFanout(exchange.ToString(), msg, noTime);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="queue"></param>
        /// <param name="action"></param>
        public static void RabbitMQReceiveMsg(string queue, Action<string> action)
        {
            try
            {
                if (runner != null)
                {
                    runner.ReceiveMsg(queue, action);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("接收消息发生错误，错误原因：" + ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="queue"></param>
        /// <param name="action"></param>
        public static void RabbitMQReceiveMsg(object queue, Action<string> action)
        {
            RabbitMQReceiveMsg(queue.ToString(), action);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="queue"></param>
        /// <param name="action"></param>
        public static void RabbitMQReceiveMsgFanout(string exchange, Action<string> action)
        {
            try
            {
                if (runner != null)
                {
                    runner.ReceiveMsgExchangeFanout(exchange, action);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("接收消息发生错误，错误原因：" + ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="exchange"></param>
        /// <param name="action"></param>
        public static void RabbitMQReceiveMsgFanout(object exchange, Action<string> action)
        {
            RabbitMQReceiveMsgFanout(exchange.ToString(), action);
        }

        /// <summary>
        /// 
        /// </summary>
        public static void RabbitMQClose()
        {
            try
            {
                if (runner != null)
                {
                    runner.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("关闭RabbitMQ消息队列服务发生错误，错误原因：" + ex.Message);
            }
        }
    }
}
